/**
* @class
* @augments ksg.ui.grid.BaseGrid
* @Depends ksg.gridCore.js
*/
ksg.ui.grid.WebServiceGrid = $.extend({}, ksg.ui.grid.BaseGrid, /** @lends ksg.ui.grid.WebServiceGrid# */{
	_internalCreate: function() {
		var options = this.options;
		this.dataHandler = ksg.ui.grid.dataHandlerFactory.webServices("id",options.serviceName, options.totalCountServiceName);
		this.templates = ksg.ui.grid.templateHandlers.tmpl;
	},
	_internalDestroy: function() {
	},

	_internalInitGrid: function() {
	},
	_internalSelectBizObj: function(bizObjId) {
		var options = this.options;
		if(typeof(options.parentBizProc) == "object")
			options.parentBizProc.triggerEvent(null, "selectBizObj", bizObjId); 
	}
});

$.widget("ksg.webServiceGrid", ksg.ui.grid.WebServiceGrid);
ksg.ui.widgetMap.addWidget("ksg-standalone-bizobj-grid", function ($el, options) { 
	$el.webServiceGrid(options); 
});

ksg.ui.grid.dataHandlerFactory.webServices = function(idFieldName, getListServiceName, getTotalCountServiceName, generalListServiceNames ) {
	return {
		/**
		* Gets a list of objects to be shown either as the primarly root list of rows or
		* as a child set of rows from a parent row being opened up via the child visibility widget.
		*/
		getList: function(id, options, onSuccess) {
			var self = this;
			var listOptions = self._serviceOptions(id, options);
			var newOnSuccess = function(json) {
				var dataList = self._getDataListFromJsonResponse(json);
				if(onSuccess)
					onSuccess(dataList.bizObjs);
			},
			newOnError = function(err) {
			};
			
			ksg.services[getListServiceName](listOptions, newOnSuccess, newOnError);
		},
		
		shouldRequestChildListData: function() {
			return true;
		},
		
		_serviceOptions: function(id, options) {
			var runtimeOptions = {};

			if(id) {
				var idFieldName = this.getIdFieldName();
				if(idFieldName != "")
					runtimeOptions[idFieldName] = id;
			}
			
			return $.extend({}, options.baseServiceOptions, runtimeOptions);
		},
		getIdFieldName: function() {
			return idFieldName;
		},

		/**
		* When the grid is only shown a subset of rows at one time, this is used to get the total count.
		* This is an optional method that is only used if the getList method doesnt also return
		* the total count.  If this method returns null or -1 then the total will not be shown.
		*/
		getTotalPossibleCount: function(options, onSuccess) {
			var self = this;
			var listOptions = self._serviceOptions("", true, true,options);
			var newOnSuccess = function(json) {
				var dataList = self._getDataListFromJsonResponse(json);
				if(onSuccess)
					onSuccess(dataList);
			},
			newOnError = function(err) {
			};
			if(getTotalCountServiceName)
				ksg.services[getTotalCountServiceName](listOptions, newOnSuccess, newOnError);
		},
		/**
		* Used by cell editors to get generate lists of data needed to help edit.  This is particularly
		* used for dropdown control cell editors, which show a list of options to set the value of the field with.
		*/
		getGeneralList: function(name,options) {
		},
		
		/**
		* When the web servie returns json, this will process the response and give back just the data list
		* ready for use by the grid.
		*/
		_getDataListFromJsonResponse: function(json) {
			return json.response;
		},
		
		hasValue: function(obj, fieldName) {
			return ksg.bizobj.BizObj.hasValue(obj,fieldName);
		},
		getValue: function(obj, fieldName, defValue) {
			return ksg.bizobj.BizObj.getValue(obj,fieldName, defValue)
		},
		setValue: function(obj, fieldName, val) {
			//TODO setup set value
		}		
	};
}
